package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.request.QuestionCreateRequest;
import com.github.permissiondog.cancanpbackend.request.QuestionDeleteRequest;
import com.github.permissiondog.cancanpbackend.request.QuestionModifyRequest;
import com.github.permissiondog.cancanpbackend.response.BaseResponse;
import com.github.permissiondog.cancanpbackend.response.QuestionResponse;
import com.github.permissiondog.cancanpbackend.service.TemplateService;
import org.apache.ibatis.exceptions.PersistenceException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminQuestionServlet", value = "/admin/template/question")
public class AdminQuestionServlet extends BaseServlet {
    /**
     * 创建审批问题
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        var body = bind(req, res, QuestionCreateRequest.class);
        if (body == null)   return;

        try {
            var question = TemplateService.getInstance().createQuestion(
                    body.templateId, body.title, body.type
            );
            json(res, QuestionResponse.builder().ok(true).question(question).build());
        } catch (PersistenceException e) {
            json(res, BaseResponse.builder().ok(false).msg("创建失败").error(e.getMessage()).build());
        }
    }

    /**
     * 修改审批问题的标题、类型
     */
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse res) throws IOException {
        var body = bind(req, res, QuestionModifyRequest.class);
        if (body == null)   return;

        try {
            var question = TemplateService.getInstance().getQuestionById(body.questionId);
            if (question == null) {
                json(res, BaseResponse.builder().ok(false).msg("未找到问题").build());
                return;
            }
            question.setTitle(body.title);
            question.setType(body.type);
            TemplateService.getInstance().modifyQuestion(question);
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("修改失败").error(e.getMessage()).build());
            return;
        }
        json(res, BaseResponse.builder().ok(true).build());
    }

    /**
     * 删除审批问题
     */

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws IOException {
        var body = bind(req, res, QuestionDeleteRequest.class);
        if (body == null)   return;

        try {
            var ok = TemplateService.getInstance().deleteQuestion(body.questionId);
            if (!ok) {
                json(res, BaseResponse.builder().ok(false).msg("问题 ID 不存在").build());
                return;
            }
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("删除失败").error(e.getMessage()).build());
            return;
        }
        json(res, BaseResponse.builder().ok(true).build());
    }
}
